﻿using Core.Context;
using Core.Dtos;
using Core.Domain;
using Core.Factories;
using Core.IServices;
using Core.Messages;
using Core.Queries;
using Library.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using Share.Enterprise.Api;

namespace Front.Areas.Admin.Controllers.Api
{
    [RoutePrefix("api/admin/roles")]
    //[Authorize(Roles = "Admin")]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class RoleApiController : ApiController, ICrudApiController<string, RoleDto, RoleQuery>, IQueryApiController<string, RoleDto, RoleQuery>
    {
        private readonly IManageRoleService service;
        private readonly IRequestFactory requestFactory;
        private readonly ICurrentContext currentContext;

        public RoleApiController(IManageRoleService service, ILogger logger, ICurrentContext currentContext, IRequestFactory requestFactory)            
        {
            this.service = service;
            this.requestFactory = requestFactory;
            this.currentContext = currentContext;
        }

        [HttpPost]
        [Route("searches")]
        public QueryResponse<RoleDto> FindBy(RoleQuery query)
        {
            Request<RoleQuery> request = requestFactory.GetInstance<RoleQuery>(query);
            return service.FindBy(request);
        }

        [Route("{id}")]
        public RoleDto Get(string id)
        {
            return service.FindByID(id);
        }

        [HttpPost]
        [Route("")]
        public virtual Response<string> Post(RoleDto dto)
        {
            Request<RoleDto> request = requestFactory.GetInstance<RoleDto>(dto);
            return service.Add(request);
        }

        [HttpPut]
        [Route("{id}")]
        public virtual Response Put(string id, RoleDto dto)
        {
            Request<RoleDto> request = requestFactory.GetInstance<RoleDto>(dto);
            return service.Edit(id, request);
        }

        [HttpDelete]
        [Route("{id}")]
        public virtual Response Delete(string id)
        {
            return service.Delete(id);
        }
    } 
}
